﻿IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'proc_cm_SavePhoto')
BEGIN
    PRINT 'Dropping Procedure proc_cm_SavePhoto'
    DROP  Procedure  proc_cm_SavePhoto
END
GO

PRINT 'Creating Procedure proc_cm_SavePhoto'
GO

CREATE Procedure dbo.proc_cm_SavePhoto
	@pPhotoId uniqueidentifier
	,@pPhotoalbumId uniqueidentifier
	,@pFilenameTxt nvarchar(255)
	,@pFilepathTxt nvarchar(512)
	,@pFilesizeNum int
	,@pTitleTxt nvarchar(100) = null
	,@pDescTxt nvarchar(1000) = null
	,@pPublicInd bit = 1
	,@pFeaturedInd bit
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

	if not exists (select * from [dbo].[tbl_photo] where [photo_id] = @pPhotoId)
	begin
		insert into [dbo].[tbl_photo]
		(
			[photo_id]
			,[photoalbum_id]
			,[filename_txt]
			,[filepath_txt]
			,[filesize_num]
			,[title_txt]
			,[desc_txt]
			,[create_dttm]
			,[public_ind]
			,[featured_ind]
		)
		values
		(
			@pPhotoId
			,@pPhotoalbumId
			,@pFilenameTxt
			,@pFilepathTxt
			,@pFilesizeNum
			,@pTitleTxt
			,@pDescTxt
			,getdate()
			,@pPublicInd
			,@pFeaturedInd
		)
	end
	else
	begin
		update
			[dbo].[tbl_photo]
		set
			[photoalbum_id] = @pPhotoalbumId
			,[filename_txt] = @pFilenameTxt
			,[filepath_txt] = @pFilepathTxt
			,[filesize_num] = @pFilesizeNum
			,[title_txt] = @pTitleTxt
			,[desc_txt] = @pDescTxt
			,[public_ind] = @pPublicInd
			,[featured_ind] = @pFeaturedInd
		where
			[photo_id] = @pPhotoId
	end
END
GO

GRANT EXEC ON dbo.proc_cm_SavePhoto TO PUBLIC
GO

